<!DOCTYPE html>
<html class="x-admin-sm" lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>任务统一调度平台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>

    <link rel="stylesheet" th:href="@{../static/layui/css/font.css}">
    <link rel="stylesheet" th:href="@{../static/layui/css/xadmin.css}">
    <script th:src="@{../static/layui/js/jquery.min.js}"></script>
    <script th:src="@{../static/js/CommonUtil.js}"></script>
    <script th:src="@{../static/js/kconfig.js}"></script>
    <script th:src="@{../static/layui/lib/layui/layui.js}" charset="utf-8"></script>
    <script th:src="@{../static/layui/js/xadmin.js}" charset="utf-8"></script>
    <script th:src="@{../static/layui/js/md5.js}" charset="utf-8"></script>

    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]--></head>

<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <input type="hidden" id="id" name="id"/>
            <div class="layui-form-item">
                <label for="newPassword" class="layui-form-label">
                    <span class="x-red">*</span>新密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="newPassword" name="newPassword" required=""
                           lay-verify="required|passwordCheck"
                           autocomplete="off"
                           class="layui-input"></div>
                <div class="layui-form-mid layui-word-aux">6到30字符</div>
            </div>
            <div class="layui-form-item">
                <label for="confirmPassword" class="layui-form-label">
                    <span class="x-red">*</span>确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="confirmPassword" name="confirmPassword" required=""
                           lay-verify="required|passwordCheck"
                           autocomplete="off"
                           class="layui-input"></div>
                <div class="layui-form-mid layui-word-aux">6到30字符</div>
            </div>
            <div class="layui-form-item">
                <label for="updateUser" class="layui-form-label"></label>
                <button class="layui-btn" id="updateUser" lay-filter="updateUser" style="width:15%;" lay-submit=""
                        type="submit">保存
                </button>
            </div>
        </form>
    </div>
</div>

<script>
    layui.use(['form', 'layer'],
        function () {
            const $ = layui.jquery;
            const form = layui.form;
            const layer = layui.layer;

            const reqParams = RequestParameter();
            if (null == reqParams) {
                history.back();
            }
            const userId = reqParams['id'];
            if (null == userId) {
                history.back();
            }

            //监听提交
            form.on('submit(updateUser)', function (data) {
                if (data.field.newPassword !== data.field.confirmPassword) {
                    ResponseMsg.errorMsg('两次输入的密码不一致');
                    return;
                }

                const md5Password = hex_md5(data.field.newPassword);
                const jsonParam = JSON.stringify({
                    id: userId,
                    newPassword: md5Password
                });

                AjaxRequest.aPost(api.user.update_pwd, jsonParam, function (r) {
                    if (r.code === 0) {
                        // 登入成功的提示与跳转
                        layer.msg('修改成功', {
                            offset: '15px', icon: 1, time: 500
                        }, function () {
                            // 获得frame索引
                            const index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            //刷新父级页面的表格
                            window.parent.location.reload();
                        });
                    } else {
                        if (r.msg !== null && r.msg !== '') {
                            ResponseMsg.errorMsg('修改失败，' + r.msg);
                        } else {
                            ResponseMsg.errorMsg('修改失败，请重试。');
                        }
                    }
                });
                return false;
            });

            //参数校验
            form.verify({
                passwordCheck: function (value, item) {
                    if (value.length < 6) {
                        return '密码长度不能小于 6 位';
                    }
                    if (value.length > 30) {
                        return '密码长度不能大于 30 位';
                    }
                },
            });
        });
</script>

</body>
</html>